/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TStock;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TStockDao;
import com.ruipengkj.service.TStockService;
/**
 * 说明：
 * 商品库存Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TStockServiceImpl implements TStockService{
	@Resource
	private TStockDao TStockDao;//DAO层
	
	//查询所有
	@Override
	public List<TStock> findService(TStock entity) throws Exception {
		String hql = "select o from TStock o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getTStore()!= null&&entity.getTStore().getStoreID()!=null){
				whereHQL.append("o.TStore=?").append(BaseQuery.AND);
				param.add(entity.getTStore());
			}
			if(entity.getTCommodity()!= null&&entity.getTCommodity().getCommodityID()!=null){
				whereHQL.append("o.TCommodity=?").append(BaseQuery.AND);
				param.add(entity.getTCommodity());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TStockDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	
	@Override
	public List<TStock> findListService(TStock entity) throws Exception {
		String hql = "select new TStock(o.stockID, o.num) from TStock o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getTStore()!= null&&entity.getTStore().getStoreID()!=null){
				whereHQL.append("o.TStore=?").append(BaseQuery.AND);
				param.add(entity.getTStore());
			}
			if(entity.getTCommodity()!= null&&entity.getTCommodity().getCommodityID()!=null){
				whereHQL.append("o.TCommodity=?").append(BaseQuery.AND);
				param.add(entity.getTCommodity());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TStockDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	
	//根据ID翻页查询
	@Override
	public PageModel<TStock> getScrollService(int firstResult,
			int maxResult, TStock entity) throws Exception {
		String hql = "select o from TStock o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getTStore()!= null&&entity.getTStore().getStoreID()!=null){
				whereHQL.append("o.TStore=?").append(BaseQuery.AND);
				param.add(entity.getTStore());
			}
			if(entity.getTCommodity()!= null&&entity.getTCommodity().getCommodityID()!=null&&!entity.getTCommodity().getCommodityID().equals("")){
				whereHQL.append("o.TCommodity=?").append(BaseQuery.AND);
				param.add(entity.getTCommodity());
			}
			if(entity.getTCommodity()!= null&&entity.getTCommodity().getName()!=null&&!entity.getTCommodity().getName().equals("")){
				whereHQL.append("o.TCommodity.name=?").append(BaseQuery.AND);
				param.add(entity.getTCommodity().getName());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TStockDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

//	@Override
//	public PageModel<TStock> getScrollListService(int firstResult,
//			int maxResult, TStock entity) throws Exception {
//		String hql = "select new TStock(o.stockID, o.num) from TStock o";
//		StringBuffer whereHQL = new StringBuffer();
//		List<Object> param = new ArrayList<Object>();
//		if (entity != null) {
//			if(entity.getTStore()!= null&&entity.getTStore().getStoreID()!=null){
//				whereHQL.append("o.TStore=?").append(BaseQuery.AND);
//				param.add(entity.getTStore());
//			}
//			if(entity.getTCommodity()!= null&&entity.getTCommodity().getCommodityID()!=null&&!entity.getTCommodity().getCommodityID().equals("")){
//				whereHQL.append("o.TCommodity=?").append(BaseQuery.AND);
//				param.add(entity.getTCommodity());
//			}
//			if(entity.getTCommodity()!= null&&entity.getTCommodity().getName()!=null&&!entity.getTCommodity().getName().equals("")){
//				whereHQL.append("o.TCommodity.name=?").append(BaseQuery.AND);
//				param.add(entity.getTCommodity().getName());
//			}
//			if(whereHQL.length()>4)
//				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
//		}
//		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
//		return TStockDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
//	}
	
	//修改
	@Override
	public void modifyService(TStock entity) throws Exception {
		TStockDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TStock entity) throws Exception {
		TStockDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TStockDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TStock getByIdService(String id) throws Exception {
		return TStockDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TStock entity) throws Exception {
		TStockDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TStock entity) throws Exception {
		return TStockDao.saveRetDao(entity);
	}
	public TStockDao getTStockDao() {
		return TStockDao;
	}

	public void setTStockDao(TStockDao TStockDao) {
		this.TStockDao = TStockDao;
	}
}